var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'expense_tran_ajs.html', controller: ListExpMstCtrl}).
      when('/deleteexp/', {templateUrl: 'expense_tran_ajs.html', controller: ListExpMstCtrl}).
      when('/editemp/', {templateUrl: 'expense_tran_ajs.html', controller: ListExpMstCtrl}). 
      when('/updtemp/', {templateUrl: 'expense_tran_ajs.html', controller: ListExpMstCtrl}). 
      when('/cancel/', {templateUrl: 'expense_tran_ajs.html', controller: ListExpMstCtrl}). 
      otherwise({redirectTo: '/'});
}]);


app.service('ContactService', function () 
	{
     this.get = function (exptranid,$window) {
        for (i in expensemast) {
            if (expensemast[i].exptranid == exptranid) {
                 $window.alert("Edit Expense Details  "+exptranid);
                return expensemast[i];
            }
        }
     };
	}
);


function ListExpMstCtrl($scope, $http,$window,ContactService,$location)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 25;
    $scope.newexpensemast = {};
	$scope.comment = "";
	$scope.expensecat = [{ id: 1, name: 'SALARY'}, { id: 2, name: 'ADVANCE'} ,{ id: 3, name: 'TELEPHONE BILL'} ,
						 { id: 4, name: 'ELECTRICITY BILL'}, { id: 5, name: 'PETROL BILL'},{ id: 6, name: 'AC MAINTENANCE'}, 
						 { id: 7, name: 'INTERNET BILL'}, { id: 8, name: 'OTHERS'} ];
	
	$scope.Math = window.Math;
    $http.get('expense_tran_ajs_selectdata.php').success(function(data) {
    $scope.expensemast = data;
    });
	
     $http.get('employeeiddata.php').success(function(data1) {
    $scope.employeeidmast = data1;
    });
	
       
     //Function to Edit Record in the Expense Master Screen 
    $scope.edit = function(exptranid,comment) {
         for(i in $scope.expensemast) {
          if($scope.expensemast[i].exptranid == exptranid)
            {
            //  $window.alert(" value of exptranid is "+i+ "  "+exptranid+" " +comment); 
			  //we use angular.copy() method to create copy of original object
               $scope.newexpensemast = angular.copy($scope.expensemast[i]);
				$scope.comment = comment;
            }
        }
            
    };
    
	
    //Function to Delete Record in the Expense Master based on the exptranid 
   $scope.deleteexp = function(item) {
      if (confirm('Do you really want to delete this Expense record?')) 
        {
            $window.alert(item+' Record is being Deleted');
            $http.post('expense_tran_ajs_deletedata.php',{'exptranid': item}).success(function(data)
            {
                $window.alert( 'Record for ' + item+ 'got deleted Successfully');
                 $scope.activePath = $location.path('expense_tran_ajs.php#/editemp/');
                 window.location.href = 'expense_tran_ajs.php#/editemp/';
            });
               
        }
      };
      
	
	//Function to Update Data in the Expense Master  
		$scope.updtexpdata = function(newexpensemast,comment) 
			{   
				$window.alert("Updation of Record for "+newexpensemast.exptranid+",  "+newexpensemast.expdate +",  "+newexpensemast.expensecat + ",  "+ newexpensemast.amtpaid +",  "+comment+ ",  "+newexpensemast.empid );
				var sql = 'expense_tran_ajs_updatedata.php?'+'empid='+newexpensemast.empid+'&expdate='+newexpensemast.expdate +'&amtpaid='+newexpensemast.amtpaid+'&expensecat='+newexpensemast.expensecat+'&comment='+comment+'&exptranid='+newexpensemast.exptranid;
				//$window.alert(sql);
				$http.post(sql).success(function(data)  
				{
					$window.alert( 'Record for expense Tran id  '+ newexpensemast.exptranid  + '  got Successfully Updated');
					window.location.href = 'expense_tran_ajs.php#/editemp/';
				});
			};
      
	 //Function to Insert Data in the Expense Master  
	   $scope.saveexpdata = function(newexpensemast,comment) 
	  	{   
            $window.alert("Save Record for "+newexpensemast.expdate +","+newexpensemast.expensecat + ","+ newexpensemast.empid + ","+ newexpensemast.amtpaid+ ","+ comment);
			var sql = 'expense_tran_ajs_insertdata.php?'+'&expdate='+newexpensemast.expdate+'&expensecat='+newexpensemast.expensecat +'&amtpaid='+newexpensemast.amtpaid+'&comment='+comment+'&empid='+newexpensemast.empid;
			//$window.alert(sql);
			$http.post(sql).success(function(data)  
			{
				$window.alert( 'Record for '+  newexpensemast.empid + "  ,  " + newexpensemast.expensecat+ 'got Successfully Saved');
				window.location.href = 'expense_tran_ajs.php#/editemp/';
			});
		};
	  
	  //Function to Clear the selected parameters in the search screen.
        $scope.cancel= function()
		{
			$scope.activePath = null;
			$scope.activePath = $location.path('expense_search_ajs.php#/editemp/');
			window.location.href = '#/editemp/';
		};
      
 }
    
    //Function to convert the entered value in UI into uppercase
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
     //Custom Filter Function to sort the record.
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
        